//
//  JXRDataCachesTool.m
//  JXRMinor
//
//  Created by dllo on 15/10/20.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "JXRDataCachesTool.h"
#import <SDImageCache.h>
@implementation JXRDataCachesTool

+ (JXRDataCachesTool *)shareJXRDataCachesTool
{
    
    static JXRDataCachesTool *dataCachesTool;
    static dispatch_once_t oneToKen;
    dispatch_once(&oneToKen, ^{
        
        dataCachesTool = [[JXRDataCachesTool alloc] init];
        [dataCachesTool createCachesFilePath];
    });
    return dataCachesTool;
}

// -- caches 文件路径 ---//
- (NSString *)cachesFilePath
{
    
    NSArray *SandBox = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cachesPath = SandBox[0];
    return cachesPath;
}

// 创建DiyArt文件
- (void)createCachesFilePath
{
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    NSString * filePath = [[self cachesFilePath] stringByAppendingPathComponent:@"JXRMinorDownload"];
    
    if (NO == [fileManager fileExistsAtPath:filePath]) {
        //如果沙盒中没有缓存的文件夹，创建文件夹
        [fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
    }
}


- (NSString *)JXRMinorDownloadFilePath
{
    
    NSArray *SandBox = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cachesPath = SandBox[0];
    
    NSString * filePath = [cachesPath stringByAppendingPathComponent:@"JXRMinorDownload"];
    
    return filePath;
}


- (float)fileSizeAtPath:(NSString*)path
{
    
    NSFileManager *fileManager=[NSFileManager defaultManager];
    
    if([fileManager fileExistsAtPath:path]){
        
        long size = [fileManager attributesOfItemAtPath:path error:nil].fileSize;
        // 返回值是字节 B K M
        
        return size/1024.0/1024.0;
    }
    
    return 0;
    
}

//计算目录大小

- (float)folderSizeAtPath
{
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    float folderSize;
    
    if ([fileManager fileExistsAtPath:[self cachesFilePath]]) {
        
        NSArray *childerFiles=[fileManager subpathsAtPath:[self cachesFilePath]];
        
        for (NSString *fileName in childerFiles) {
            
            NSString *absolutePath = [[self cachesFilePath] stringByAppendingPathComponent:fileName];
            
            // 计算单个文件大小
            folderSize += [self fileSizeAtPath:absolutePath];
        }
        
        //SDWebImage框架自身计算缓存的实现
        folderSize += [[SDImageCache sharedImageCache] getSize]/1024.0/1024.0;
        
        return folderSize;
    }
    return 0;
}

//清理缓存文件

//同样也是利用NSFileManager API进行文件操作，SDWebImage框架自己实现了清理缓存操作，我们可以直接调用。
- (void)clearCache
{
    
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    NSArray *childerFiles=[fileManager subpathsAtPath:[self cachesFilePath]];
    
    for (NSString *fileName in childerFiles) {
        
        NSString *absolutePath = [[self cachesFilePath] stringByAppendingPathComponent:fileName];
        [fileManager removeItemAtPath:absolutePath error:nil];
        
    }
    // SDImageCache 自带缓存
    [[SDImageCache sharedImageCache] cleanDisk];
    
}

// 归档
- (void)saveCachesWithPlistNameUrl:(NSString *)url responseObject:(id)responseObject{
    
    NSString *cacheUrl = [NSString stringWithFormat:@"%@.plist",url];
    
    NSString *new = [cacheUrl stringByReplacingOccurrencesOfString:@"/" withString:@""];
    
    NSString *newUrl = [new stringByReplacingOccurrencesOfString:@"http:" withString:@""];
    
    
    NSString *cachesPlistPath = [[self JXRMinorDownloadFilePath] stringByAppendingPathComponent:newUrl];
    
    [NSKeyedArchiver archiveRootObject:responseObject toFile:cachesPlistPath];
    NSLog(@"缓存文件地址:%@",cachesPlistPath);
}

// 反归档
- (id)cachesWithurl:(NSString *)url{
    
    NSString *cacheUrl = [NSString stringWithFormat:@"%@.plist",url];
    
    NSString *new = [cacheUrl stringByReplacingOccurrencesOfString:@"/" withString:@""];
    
    NSString *newUrl = [new stringByReplacingOccurrencesOfString:@"http:" withString:@""];
    
    
    NSString *cachesPlistPath = [[self JXRMinorDownloadFilePath] stringByAppendingPathComponent:newUrl];
    
    id result = [NSKeyedUnarchiver unarchiveObjectWithFile:cachesPlistPath];
    //NSLog(@"反归档寻找文件文件地址:%@",cachesPlistPath);
    return result;
}



@end
